import matplotlib.pyplot as plt

import list_usd_rus
import io, copy

import PIL
import PIL.Image

def draw_graph_with_name(plot1, plot2=[], plot3=[], plot4=[], color=(1,1,1)):
	fig, ax = plt.subplots()
	fig.set_figwidth(4)
	fig.set_figheight(2.5)
	ax.set_facecolor(color)


	ax.plot(plot1[0], plot1[1], label=plot1[2])
	if plot2 != []:
		ax.plot(plot2[0], plot2[1], label=plot2[2])
	if plot3 != []:
		ax.plot(plot3[0], plot3[1], label=plot3[2])
	if plot4 != []:
		ax.plot(plot4[0], plot4[1], label=plot4[2])

	ax.legend()
	img_buf = io.BytesIO()
	plt.savefig(img_buf, format='jpg', dpi=720, bbox_inches='tight')
	plt.close()
	plt.cla()
	plt.clf()
	#exit()
	return PIL.Image.open(img_buf)

def draw_graph_with_color(plots, bg_color=(0,0,0), xtext_rotation=0, font_size=10):
	plt.rcParams.update({'font.size': font_size})

	fig, ax = plt.subplots()
	fig.set_figwidth(4)
	fig.set_figheight(1)
	ax.set_facecolor(bg_color)
	ax.tick_params(axis='x', rotation=xtext_rotation)
	#fig.patch.set_facecolor(bg_color)


	for plot in plots:
		ax.plot(plot[0], plot[1], color=plot[2])

	#ax.legend()
	img_buf = io.BytesIO()
	plt.savefig(img_buf, format='jpg', dpi=200, bbox_inches='tight')
	plt.close()
	plt.cla()
	plt.clf()
	#exit()
	return PIL.Image.open(img_buf)

def cv2_show():
	import cv2, numpy
	i = 50
	while 1:
		if i >= 100:
			i = 50
		i+=1
		rub = list_usd_rus.lst()[:i]
		date = list_usd_rus.lst_only_date()[:i]
		#lt = time.time()
		pil_image = draw_graph_with_color([[date, rub, (1,1,1)]], xtext_rotation=90, font_size=5)
		#print((time.time()-lt)*1000)
		cvimg = numpy.array(pil_image)[:, :, ::-1].copy()
		cv2.imshow('kdf', cvimg)
		cv2.waitKey(1)

#cv2_show()
